<?php

/**
 * order actions.
 *
 * @package    emc
 * @subpackage order
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class orderActions extends sfActions
{
	/**
	* Executes index action
	*
	* @param sfRequest $request A request object
	*/
	public function executeIndex(sfWebRequest $request)
	{
		if ($this->getUser()->hasAttribute("cart"))
			$cart = $this->getUser()->getAttribute("cart");
		else 
			$this->forward('default', 'index');
		
		if (count($cart) == 0)
			$this->forward('default', 'index');
		
		$delivery_address = $request->getParameter("delivery_address");
		if (isset($delivery_address) && $delivery_address)
		{
			$payment_method = $request->getParameter("payment_method");
			$cbo_is_address_default = $request->getParameter("cbo_is_address_default");
			$customer_tel = $request->getParameter("customer_tel");
			
			$order = new ClientOrder();
			$order->user_id = $this->getUser()->getGuardUser()->id;
			$order->delivery_address = $delivery_address;
			$order->payment_method = $payment_method;
			$order->save();
			
			if (count($cart) > 0)
			{
				foreach ($cart as $cart_item) 
				{
					$order_product = new OrderProduct();
					$order_product->order_id = $order->id;
					$order_product->product_id = $cart_item['product_id'];
					$order_product->option_id = $cart_item['option_id'];
					$order_product->quantity = $cart_item['quantity'];
					$order_product->save();
				}
			}
			
			$user = $this->getUser()->getGuardUser();
			if (isset($cbo_is_address_default) && $cbo_is_address_default == "on") 
				$user->address = $delivery_address;

			$user->tel = $customer_tel;
			$user->save();
			
			$this->order_notify = "success";
			$cart = array();
			$this->getUser()->setAttribute("cart", $cart);
			
			
			
		}
		
		return sfView::SUCCESS;
	}
}
